package cn.wgx.modules.sys.services;

import cn.wgx.commons.util.IdGen;
import cn.wgx.commons.util.StringUtils;
import cn.wgx.commons.util.Validation;
import cn.wgx.modules.base.services.BaseService;
import cn.wgx.modules.sys.entity.SysMsg;
import cn.wgx.modules.sys.entity.SysMsgExample;
import cn.wgx.modules.sys.entity.SysUser;
import cn.wgx.modules.sys.entity.dto.DTOSysMsg;
import cn.wgx.modules.sys.entity.mapper.SysMsgMapper;
import cn.wgx.modules.sys.util.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysMsgService extends BaseService<SysMsg> {

    @Resource
    SysMsgMapper sysMsgMapper;

    @Resource
    SysUserService sysUserService;


    @Transactional(readOnly = true)
    public List<SysMsg> getList(SysMsg sysMsg, boolean desc) {
        SysMsgExample example = new SysMsgExample();
        SysMsgExample.Criteria criteria = example.createCriteria();
        if(sysMsg.getDel() == null){
            criteria.andDelEqualTo(0);
        }else{
            criteria.andDelEqualTo(sysMsg.getDel());
        }
        if (sysMsg.getType() != null) {
            criteria.andTypeEqualTo(sysMsg.getType());
        }
        if (sysMsg.getStatus() != null) {
            criteria.andStatusEqualTo(sysMsg.getStatus());
        }
        if (StringUtils.isNotEmpty(sysMsg.getTitle())) {
            criteria.andTitleLike("%" + sysMsg.getTitle() + "%");
        }
        if (StringUtils.isNotEmpty(sysMsg.getContent())) {
            criteria.andContentLike("%" + sysMsg.getContent() + "%");
        }
        if(sysMsg.getIsSend() != null){
            criteria.andIsSendEqualTo(sysMsg.getIsSend());
        }
        if(Validation.isNotNullOrZero(sysMsg.getToUserId() )){
            criteria.andToUserIdEqualTo(sysMsg.getToUserId());
        }
        if(Validation.isNotNullOrZero(sysMsg.getFromUserId() )){
            criteria.andFromUserIdEqualTo(sysMsg.getFromUserId());
        }
        example.setOrderByClause("msg_id desc");
        return sysMsgMapper.selectByExample(example);
    }

    @Transactional(readOnly = true)
    public List<SysMsg> getListByIds(List<Integer> ids) {
        if(ids == null || ids.isEmpty()){
            return emptyList;
        }
        List<Integer> uniList = getUniList(ids);
        if(!uniList.isEmpty()){
            SysMsgExample example = new SysMsgExample();
            SysMsgExample.Criteria criteria = example.createCriteria();
            criteria.andMsg_idIn(uniList);
            List<SysMsg> eduCourseAssignmentHis = sysMsgMapper.selectByExample(example);
            return eduCourseAssignmentHis;
        }
        return null;
    }


    public DTOSysMsg wrap(SysMsg sysMsg){
        DTOSysMsg dtoSysMsg = new DTOSysMsg();
        BeanUtils.copyProperties(sysMsg, dtoSysMsg);
        if(Validation.isNotNullOrZero(dtoSysMsg.getFromUserId())){
            SysUser userById = UserUtil.getUserById(dtoSysMsg.getFromUserId());
            if(userById != null){
                dtoSysMsg.set_fromUser(userById);
            }
        }
        return  dtoSysMsg;
    }


    @Transactional(readOnly = true)
    public SysMsg getOne(Integer msg_id) {
        return sysMsgMapper.selectByPrimaryKey(msg_id);
    }

    @Transactional
    public int updateStatus(Integer msg_id) {
        SysMsgExample example = new SysMsgExample();
        example.createCriteria().andMsg_idEqualTo(msg_id).andToUserIdEqualTo(UserUtil.getUser().getUser_id())
                .andStatusEqualTo(0)
                .andIsSendEqualTo(false);
        SysMsg sysMsg = new SysMsg();
        sysMsg.setStatus(1);
        return sysMsgMapper.updateByExampleSelective(sysMsg, example);
    }

    @Transactional
    public int updateNum(Integer msg_id, Integer num) {
        if(Validation.isNotNullOrZero(msg_id) && num != null){
            SysMsgExample example = new SysMsgExample();
            example.createCriteria().andMsg_idEqualTo(msg_id);
            SysMsg sysMsg = new SysMsg();
            sysMsg.setStatus(num);
            return sysMsgMapper.updateByExampleSelective(sysMsg, example);
        }
        return 0;
    }

    @Transactional
    public int delPublish(String hashCode, Integer fromUserId) {
        SysMsgExample example = new SysMsgExample();
        example.createCriteria().andHashCodeEqualTo(hashCode).andFromUserIdEqualTo(fromUserId);
        return sysMsgMapper.deleteByExample(example);
    }

    @Transactional
    public int deleteBatch(List<Integer> msg_ids, boolean logic) {
        int count = 0;
        SysUser user = UserUtil.getUser();
        if (logic) {//逻辑删除 del = 1
            SysMsg sysMsg = new SysMsg();
            for (Integer msg_id : msg_ids) {
                SysMsgExample example = new SysMsgExample();
                example.createCriteria().andMsg_idEqualTo(msg_id).andDelEqualTo(0)
                        .andToUserIdEqualTo(user.getUser_id()).andIsSendEqualTo(false);
                sysMsg.setDel(1);
                count += sysMsgMapper.updateByExampleSelective(sysMsg, example);
            }
        } else {//彻底删除
            SysMsgExample example = new SysMsgExample();
            for (Integer msg_id : msg_ids) {
                example.createCriteria().andMsg_idEqualTo(msg_id).andDelEqualTo(1)
                        .andToUserIdEqualTo(user.getUser_id()).andIsSendEqualTo(false);
                count += sysMsgMapper.deleteByExample(example);
            }
        }
        return count;
    }

    @Transactional
    public int insertOne(SysMsg sysMsg) {
        return insertOne(sysMsg, true);
    }

    @Transactional
    public int insertOne(SysMsg sysMsg, boolean filter) {
        if(filter){
            filter(sysMsg, true);
        }
        sysMsg.set_result(sysMsgMapper.insertSelective(sysMsg));
        return sysMsg.get_result();
    }

    public void filter(SysMsg sysMsg,  boolean insert){
        if(insert){
            sysMsg.setDel(null);
            sysMsg.setStatus(null);
            if(StringUtils.isBlank(sysMsg.getHashCode())){
                sysMsg.setHashCode(IdGen.uuid());
            }
            if(!Validation.isNotNullOrZero(sysMsg.getFromUserId())){
                sysMsg.setFromUserId(UserUtil.getUser().getUser_id());
            }
        }else {

        }
        super.filter(sysMsg, insert);
    }

}